package LC;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/single-number/description/
 * Given an array of integers, every element appears twice except for one. Find that single one.
 * Note:
 * Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
 */
public class LC_136_SingleNumber_Sort {
    public static void main(String[] args) {
        System.out.println(Solution.singleNumber(new int[]{1, 2, 1, 2, 3}));
    }

    static class Solution {
        static int singleNumber(int[] nums) {
            if (nums.length == 1) {
                return nums[0];
            } else if (nums.length % 2 == 0) {
                return 0;
            } else {
                Arrays.sort(nums);
                int n = nums.length - 1;
                for (int i = 0; i < n; i += 2)
                    if (nums[i] != nums[i + 1]) return nums[i];
                return nums[n];
            }
        }
    }
}
